<template>
  <div class="login-wrap">
    <el-form class="login-container">
      <h1 class="title">登录界面</h1>
      <el-form-item>
        <el-input
          type="text"
          placeholder="用户账号"
          v-model="username"
          autocomplete="off"
        ></el-input>
      </el-form-item>
      <el-form-item>
        <el-input
          type="password"
          placeholder="用户密码"
          v-model="password"
          autocomplete="off"
        ></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="doLogin()" style="width: 100%"
          >登录</el-button
        >
      </el-form-item>
      <el-row style="text-align: center">
        <el-link @click="gotoRegister">注册</el-link>
        <el-link>忘记密码</el-link>
      </el-row>
    </el-form>
  </div>
</template>

<script setup>
import { ref } from "vue";
import { user_login } from "../utils/api";
import { useRouter } from "vue-router";

var $router = useRouter();
let username = ref("");
let password = ref("");

var gotoRegister = function () {
  $router.push("/register");
};
var doLogin = function () {
  if (username.value == null) {
    console.log("请输入账号");
  } else if (password.value == null) {
    console.log("请输入密码");
  } else {
    let params = {
      username: username.value,
      password: password.value,
    };
    user_login(params).then((res) => {
      // 登录成功
      if (res.data.meta.status == 200) {
        console.log("登陆成功");
        // 跳转
        localStorage.setItem("token", res.data.token);
        localStorage.setItem("userinfo", JSON.stringify(res.data.userinfo));

        $router.push("/index/home");
      } else {
        console.log("登陆失败");
      }
    });
  }
};
</script >

<!-- scoped="scoped" 表示当前界面有用 -->
<style scoped>
.login-wrap {
  box-sizing: border-box;
  width: 100%;
  height: 730px;
  padding-top: 10%;
  background-color: #1890ff;
}

.login-container {
  border-radius: 10px;
  margin: 0px auto;
  width: 350px;
  padding: 30px 35px 15px 35px;
  background: #fff;
  border: 1px solid #eaeaea;
  text-align: left;
  box-shadow: 0 0 20px 2px rgba(0, 0, 0, 0.1);
}

.title {
  margin: 0px auto 40px auto;
  text-align: center;
  color: #505458;
}
</style>

